package com.dunqi.gpm.shunqing.home.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;

import com.dunqi.gpm.shunqing.R;
import com.dunqi.gpm.shunqing.common.api.ApiFactory;
import com.dunqi.gpm.shunqing.common.api.RxSchedulers;
import com.dunqi.gpm.shunqing.common.base.BaseAct;
import com.dunqi.gpm.shunqing.common.base.ComObserver;
import com.dunqi.gpm.shunqing.common.base.Data;
import com.dunqi.gpm.shunqing.common.util.CommonUtil;
import com.dunqi.gpm.shunqing.common.view.ComTitleBar;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.disposables.Disposable;

public class WarningAuditActivity extends BaseAct {

    public static final int RESULT_CODE = 235;
    private static final String REPORTTYPE = "reportType";
    private static String PROPLANPROGRESSID = "proplanprogressid";
    private static String PROJECTNAME = "projectname";
    @BindView(R.id.yes_rb)
    RadioButton yesRb;
    @BindView(R.id.no_rb)
    RadioButton noRb;
    @BindView(R.id.blue_rb)
    RadioButton blueRb;
    @BindView(R.id.yellow_rb)
    RadioButton yellowRb;
    @BindView(R.id.red_rb)
    RadioButton redRb;
    @BindView(R.id.remark_edit)
    EditText remarkEdit;
    @BindView(R.id.title_bar)
    ComTitleBar titleBar;
    @BindView(R.id.container)
    View container;

    private String proPlanProgressId;
    private String projectName;
    private int reportType;

    @Override
    protected void refreshData(Bundle savedInstanceState) {

    }

    @Override
    protected void initUiAndListener() {
        proPlanProgressId = getIntent().getStringExtra(PROPLANPROGRESSID);
        projectName = getIntent().getStringExtra(PROJECTNAME);
        reportType = getIntent().getIntExtra(REPORTTYPE, 0);

        titleBar.setTitle(projectName);

        if (reportType == 1) {
            container.setVisibility(View.GONE);
        }

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_warning_audit;
    }

    public static void start(Activity context, String proPlanProgressId, String projectName, int reportType) {
        Intent intent = new Intent(context, WarningAuditActivity.class);
        intent.putExtra(PROPLANPROGRESSID, proPlanProgressId);
        intent.putExtra(PROJECTNAME, projectName);
        intent.putExtra(REPORTTYPE, reportType);
        context.startActivityForResult(intent, 100);
    }

    @OnClick(R.id.commit_tv)
    public void onViewClicked() {
//        /提交
        final ProgressDialog dialog = ProgressDialog.show(this, "提示", "正在提交 ");
        HashMap<String, String> hashMap = new HashMap<>();
        int auditStatus;
        String auditStatusText;
        if (yesRb.isChecked()) {
            auditStatus = 2;
            auditStatusText = "同意";
        } else {
            auditStatus = 3;
            auditStatusText = "不同意";
        }
        hashMap.put("proPlanProgressId", proPlanProgressId);
        hashMap.put("auditStatusText", auditStatusText);
        hashMap.put("auditStatus", String.valueOf(auditStatus));
        int smsAlertType = 0;

        if (blueRb.isChecked()) {
            smsAlertType = 1;
        }
        if (yellowRb.isChecked()) {
            smsAlertType = 2;
        }
        if (redRb.isChecked()) {
            smsAlertType = 3;
        }
        if (reportType != 1) {
            hashMap.put("smsAlertType", String.valueOf(smsAlertType));
        }
        hashMap.put("auditRemark", remarkEdit.getText().toString().trim());

        ApiFactory.getInstance()
                .submitProgressApproval(hashMap)
                .compose(RxSchedulers.<Data>io_main())
                .subscribe(new ComObserver() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addDisposables(d);
                    }

                    @Override
                    public void onSuccess(Object o) {
//                        if (yesRb.isChecked()) {
//                            setResult(200);
//                        } else {
//                            setResult(400);
//                        }
                        setResult(RESULT_CODE);
                        CommonUtil.showSingleToast("数据提交成功");
                        finish();
                    }

                    @Override
                    public void onFinal() {
                        dialog.dismiss();
                    }
                });
    }

}
